$("#head").load("header.html",function () {
})
$("#foot").load("footer.html",function () {})
new Vue({
    el:'#search',
    data:{
        list1:false,
        list2:false,
        prompt:true,
        result:'',
        //公用url
        url:'http://api.456mobi.com/upload/',
        //数据
        block:[],
        //快讯保存
        newsFlash:[],
        //资讯保存
        information:[],
        news_list:[],//新闻列表
        arr:[],
        choose:'choose'
    },
    ready:function(){
        var that = this
        $('.tabs>div').click(function () {
            if($(this).html() == '资讯'){
                $('.tabs .arrow').css({'left':'10px'})
                if(that.block.length<1){
                    that.list1 = false
                    that.prompt = true
                    that.list2 = false

                }else{
                    that.list1 = true
                    that.prompt = false
                    that.list2 = false
                }
            }else{
                $('.tabs .arrow').css({'left':'34%'})
                if(that.news_list.length<1){
                    that.list1 = false
                    that.prompt = true
                    that.list2 = false

                }else{
                    that.list1 = false
                    that.prompt = false
                    that.list2 = true
                }
            }
        })
        var scr = function () {
            var winPos = $(window).scrollTop();
            if(winPos >= 400){
                $('#totop').css({'display':'inline-block'})
            }else{
                $('#totop').css({'display':'none'})
            }
        }
        $(window).scroll(function(){
            scr();
        })
        scr();
    },
    methods:{
        down : function(id,e){
            if($("#" + id).css('maxHeight') == '88px'){
                $("#" + id).css({'-webkit-line-clamp':'20'})
                $("#" + id).css({'maxHeight':'500px'})
                $('#' + e.target.id).css({"transform":"rotate(180deg)"})
            }else{
                $("#" + id).css({'-webkit-line-clamp':'3'})
                $("#" + id).css({'maxHeight':'88px'})
                $('#' + e.target.id).css({"transform":"rotate(0deg)"})
            }
        },
        bull:function (id) {
            var self= this
            var count= parseInt($("#bull"+id+" span").html());
            count+=1;
            $("#bull"+id+" span").html(count);

            $.ajax({
                url :"http://api.456mobi.com/api/?s=home/Information/evaluate",
                type : 'post',
                data : {
                    newsflash_id : id,
                    sign:'bull_vote',
                },
                success: function(ret){
                    count = parseInt(ret.data.bull_vote);
                    var bad = parseInt(ret.data.bad_vote);
                    $("#bull"+id+" span").html(count);
                    $("#bad"+id+" span").html(bad);

                    $("#bad"+id+" .li_kong1").hide();
                    $("#bad"+id+" .li_kong4").hide();
                    $("#bad"+id+" .li_kong2").hide();
                    $("#bad"+id+" .li_kong3").show();

                    if(ret.data.user_data.bull_vote == 1){
                        $("#bull"+id+" .li_hao1").hide();
                        $("#bull"+id+" .li_hao2").hide();
                        $("#bull"+id+" .li_hao3").hide();
                        $("#bull"+id+" .li_hao4").show();
                    }else{
                        $("#bull"+id+" .li_hao4").hide();
                        $("#bull"+id+" .li_hao2").hide();
                        $("#bull"+id+" .li_hao1").hide();
                        $("#bull"+id+" .li_hao3").show();
                    }
                }
            })
        },
        bad:function (id) {
            var count = parseInt($("#bad"+id+" span").html());
            count+=1;
            $("#bad"+id+" span").html(count);
            $("#bad"+id+" .li_kong2").show();
            $("#bad"+id+" .li_kong1").hide();
            $.ajax({
                url :"http://api.456mobi.com/api/?s=home/Information/evaluate",
                type : 'post',
                data : {
                    newsflash_id : id,
                    sign:'bad_vote',
                },
                success: function(ret){
                    count = parseInt(ret.data.bad_vote);
                    var bull = parseInt(ret.data.bull_vote)
                    $("#bad"+id+" span").html(count);
                    $("#bull"+id+" span").html(bull);
                    $("#bull"+id+" .li_hao1").hide();
                    $("#bull"+id+" .li_hao4").hide();
                    $("#bull"+id+" .li_hao2").hide();
                    $("#bull"+id+" .li_hao3").show();

                    if(ret.data.user_data.bad_vote == 1){
                        $("#bad"+id+" .li_kong1").hide();
                        $("#bad"+id+" .li_kong3").hide();
                        $("#bad"+id+" .li_kong2").hide();
                        $("#bad"+id+" .li_kong4").show();
                    }else{
                        $("#bad"+id+" .li_kong4").hide();
                        $("#bad"+id+" .li_kong1").hide();
                        $("#bad"+id+" .li_kong2").hide();
                        $("#bad"+id+" .li_kong3").show();
                    }
                }
            })
        },
        more:function () {
            var that = this
            $.ajax({
                url:'http://api.456mobi.com/api/?s=home/Information/search_info',
                type:'post',
                data:{
                    type : 1,
                    keyword: that.result.data.keyword,
                    id : that.block[that.block.length-1].id
                },
                success: function(response){
                    var res = response
                    if(res.msg == '获取成功'){
                        res.data.list.forEach(function (item,index) {
                            item.more = JSON.parse(item.more)
                            var time = new Date(item.published_time * 1000).toLocaleString().split(' ')
                            time[0] = time[0].replace(/\//g,"-").substr(5,time[0].length)
                            time[1] = time[1].substring(2,time[1].length)
                            time[1] = time[1].split(':')
                            item.published_time = time[0] + ' ' + time[1][0] + ':' + time[1][1]
                            item.post_title = item.post_title.replace(new RegExp(res.data.keyword,'g'),'<span>'+res.data.keyword+'</span>')
                            that.block.push(item)
                        })
                    }else{
                        alert(res.msg)
                    }
                }
            })
        },
        newsmore:function () {
            var self = this;
            $.ajax({
                url:'http://api.456mobi.com/api/?s=home/Information/search_info',
                type:'post',
                data:{
                    type : 2,
                    keyword: self.result.data.keyword,
                    id :self.news_list[self.news_list.length-1].news_list2[self.news_list[self.news_list.length-1].news_list2.length-1].id
                },
                success: function(ret){
                    if(ret.msg == '获取成功'){
                        var arr = ret.data.list;
                        $.each(arr, function(index,val) {
                            var data = {};
                            var timestamp = Date.parse(index);
                            data.issue_time = self.timestampToTime_year(timestamp/1000);
                            data.date_str = self.timestampToTime_is(index);
                            data.week = self.timestampToTime_week(timestamp/1000);
                            if(self.news_list[self.news_list.length-1].issue_time!=data.issue_time){
                                // alert(self.news_list[self.news_list.length-1].issue_time+"//"+data.issue_time)
                                self.news_list.push(data)
                                Vue.set(self.news_list[self.news_list.length-1],'news_list2',[])
                            }
                            $.each(val, function(index2,val2) {
                                var data2 = {}
                                data2.id = val2.id;
                                data2.newsflash_id = val2.newsflash_id;
                                data2.title = val2.title.replace(new RegExp(self.result.data.keyword,'g'),'<span>'+ self.result.data.keyword+'</span>')
                                data2.content = val2.content.replace(new RegExp(self.result.data.keyword,'g'),'<span>'+ self.result.data.keyword+'</span>')
                                data2.source = val2.source;
                                data2.issue_time = val2.issue_time;
                                data2.bad_vote = val2.bad_vote;
                                data2.bull_vote = val2.bull_vote;
                                data2.evaluate = val2.evaluate;
                                // self.news_list[self.news_list.length-1].news_list2.push(data2);
                                Vue.set(self.news_list[self.news_list.length-1].news_list2,self.news_list[self.news_list.length-1].news_list2.length,data2)
                            })
                        });
                    }else if (ret.msg=='暂无更多'){
                        $('.btn').css({'color':'#aaa','border':"2px solid #aaa"})
                        $('.btn').html('暂无更多')
                    }else{
                        alert(ret.msg)
                    }
                }
            })
        },
        timestampToTime_year :function (timestamp) {
            var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            Y = date.getFullYear() + '-';
            M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            D = date.getDate() + ' ';
            h = date.getHours() + ':';
            m = date.getMinutes() + ':';
            s = date.getSeconds();
            return Y+M+D;
        },
        timestampToTime_fs :function (timestamp) {
            var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            Y = date.getFullYear() + '-';
            M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            D = date.getDate() + ' ';
            h = date.getHours() + ':';
            m = date.getMinutes() + '';
            s = date.getSeconds();
            return h+m;
        },
        timestampToTime_is:function (timestamp) {
            var self = this;
            var now = new Date();
            var time = new Date(new Date(timestamp));
            D = now.getDate();
            D2 = time.getDate();
            if(D==D2){
                return "今天"
            }else if(D>D2){
                if(D-D2>2){
                    return ""
                }
                return "昨天"
            }

        },
        timestampToTime_week :function (timestamp) {
            var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            W= date.getDay();
            if(W==0)  W="星期日"
            if(W==1)  W="星期一"
            if(W==2)  W="星期二"
            if(W==3)  W="星期三"
            if(W==4)  W="星期四"
            if(W==5)  W="星期五"
            if(W==6)  W="星期六"
            return W;
        },
        search:function () {
            var that = this
            var self = this
            if($('#input').val() == ''){
                alert('请输入搜索内容')
                return;
            }
            that.block = []
            that.news_list = []
            that.arr = []
            $.ajax({
                url:'http://api.456mobi.com/api/?s=home/Information/search_all',
                type:'post',
                data:{
                    keyword:$('#input').val()
                },
                success: function(response){
                    if(response.code == '1'){
                        var res = response
                        that.result = response
                        res.data.zx.list.forEach(function (item,index) {
                            item.more = JSON.parse(item.more)
                            var time = new Date(item.published_time * 1000).toLocaleString().split(' ')
                            time[0] = time[0].replace(/\//g,"-").substr(5,time[0].length)
                            time[1] = time[1].substring(2,time[1].length)
                            time[1] = time[1].split(':')
                            item.published_time = time[0] + ' ' + time[1][0] + ':' + time[1][1]
                            item.post_title = item.post_title.replace(new RegExp(res.data.keyword,'g'),'<span>'+res.data.keyword+'</span>')
                        })
                        that.block = res.data.zx.list
                        $.each(res.data.kx.list, function(index,val) {
                            var data = {};
                            var timestamp = Date.parse(index);
                            data.issue_time = self.timestampToTime_year(timestamp/1000);
                            data.date_str = self.timestampToTime_is(index);
                            self.arr.push(index);
                            var length=self.arr.length;
                            if(self.arr[length-2]!=index){
                                $(".news_list_box span.append").append("<p>"+index+"</p>");
                            }
                            data.week = self.timestampToTime_week(timestamp/1000);
                            self.news_list.push(data);

                            self.news_list.forEach(function (item,index) {
                                Vue.set(self.news_list[index],'news_list2',[])
                                console.log(self.news_list)
                                $.each(val, function(index2,val2) {
                                    var data2 = {}
                                    data2.id = val2.id;
                                    data2.newsflash_id = val2.newsflash_id;
                                    data2.title = val2.title.replace(new RegExp(res.data.keyword,'g'),'<span>'+res.data.keyword+'</span>')
                                    data2.content = val2.content.replace(new RegExp(res.data.keyword,'g'),'<span>'+res.data.keyword+'</span>')
                                    data2.source = val2.source;
                                    data2.issue_time = val2.issue_time;
                                    data2.bad_vote = val2.bad_vote;
                                    data2.bull_vote = val2.bull_vote;
                                    data2.evaluate = val2.evaluate;
                                    item.news_list2.push(data2);
                                })
                            })

                        });
                        if(that.block.length<1&&that.news_list.length<1){
                            that.list1 = false
                            that.list2 = false
                            that.prompt = true
                        }else{
                            if($('.tabs .arrow').css('left') == '10px'){
                                if(that.block.length<1){
                                    that.list1 = false
                                    that.list2 = false
                                    that.prompt = true
                                }else{
                                    that.list1 = true
                                    that.list2 = false
                                    that.prompt = false
                                }

                            }else{
                                if(that.news_list.length<1){
                                    that.list1 = false
                                    that.list2 = false
                                    that.prompt = true
                                }else{
                                    that.list1 = falses
                                    that.list2 = true
                                    that.prompt = false
                                }
                            }
                        }
                    }else{
                        alert('资讯获取失败')
                    }
                }
            })
        },
        news_details:function (id) {
            $(location).attr('href', 'news_details.html?id='+id);
        },
    }
});